package com.hmy.material.service.impl;

import com.hmy.material.domain.HmyMateriaFeedConsume;
import com.hmy.material.mapper.ConsumeMapper;
import com.hmy.material.mapper.StoreMapper;
import com.hmy.material.service.ConsumeService;
import com.hmy.park.domain.HmyPhouse;
import com.hmy.staff.domain.HmyStaff;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zxh
 * @version 1.0
 * @description: 物料消耗表业务service接口实现类
 * @date 2024/2/1 11:51
 */
@Service
public class ConsumeServiceImpl implements ConsumeService {
    @Resource
    private ConsumeMapper consumeMapper;

    @Resource
    private StoreMapper storeMapper;

    @Override
    public List<HmyMateriaFeedConsume> selectConsumeList(Integer materiaFeedStorePosition, Integer materiaFeedType, String creationDateStart, String creationDateEnd, String phouseCode) {
        return consumeMapper.selectConsumeList(materiaFeedStorePosition, materiaFeedType, creationDateStart, creationDateEnd, phouseCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addConsume(HmyMateriaFeedConsume consume) {
        //对存储表进行修改
        //从添加对象中拿取到仓库地点、饲料类型、消耗数量
        String storePosition = consume.getMateriaFeedStorePosition();
        String feedType = consume.getMateriaFeedType();
        long consumeNum = consume.getMateriaFeedConsumeNum();
        //根据地点和饲料获取原有的存储数量
        long storeNum = storeMapper.findStoreByTypePosition(feedType, storePosition).getMateriaFeedStoreNum();
        //用原有的数量减去消耗的数量
        long num = storeNum - consumeNum;
        //调用存储service中的方法，对存储表进行更新
        storeMapper.updateStoreNumByConsume(num, storePosition, feedType);
        return consumeMapper.addConsume(consume);
    }

    @Override
    public HmyMateriaFeedConsume selectConsumeById(Long materiaFeedConsumeId) {
        return consumeMapper.selectConsumeById(materiaFeedConsumeId);
    }

    @Override
    public int updateConsume(HmyMateriaFeedConsume consume) {
        return consumeMapper.updateConsume(consume);
    }

    @Override
    public int delConsumeByIds(Long[] materiaFeedConsumeIds) {
        return consumeMapper.delConsumeByIds(materiaFeedConsumeIds);
    }


    @Override
    public List<HmyPhouse> findAllPhouseCode() {
        return consumeMapper.findAllPhouseCode();
    }

    @Override
    public List<HmyStaff> findAllStaffName() {
        return consumeMapper.findAllStaffName();
    }
}
